package com.sztle.admodule.resp;


import com.sztle.admodule.outstatus.AppStatusProcessor;
import com.sztle.admodule.control.config.UrlConfig;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitManager {

    private static Map<String, Retrofit> sRetrofit = new HashMap<>();

    private static String getHostBase() {
        if(AppStatusProcessor.isDebug()){
            return UrlConfig.TEST_URL;
        }
        return UrlConfig.BASE_URL;
    }

    public static Retrofit getInstance() {
        return getInstance(getHostBase());
    }

    public static Retrofit getInstance(String url) {
        if (!sRetrofit.containsKey(url)) {
            synchronized (RetrofitManager.class) {
                if (!sRetrofit.containsKey(url)) {
                    Retrofit retrofit = new Retrofit.Builder()
                            .client(new CustomOkHttpClient().getOkHttpClient())
                            .addConverterFactory(GsonConverterFactory.create())
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .baseUrl(url)
                            .build();

                    sRetrofit.put(url, retrofit);
                }
            }
        }
        return sRetrofit.get(url);
    }
}
